home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / c_dates.zip / DAYS.C < prev    next >
Text File  |  1987-05-29  |  4KB  |  132 lines

  1. /************************************************************************/
  2. /*  (c)  1987 by James N. Seed,  Dallas, TX  -  all rights reserved.    */
  3. /*                                    */
  4. /*  This program is provided for example purposes only.  It, and/or    */
  5. /*  its executable equivalent, may not be sold or distributed in any    */
  6. /*  form for profit, without the prior written consent of the author.    */
  7. /************************************************************************/
  8.  
  9. #define LINT_ARGS                /* wholly advisable!!! */
  10.  
  11. #pragma check_stack-                /* special for MSC */
  12.  
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <c_dates.h>
  17.  
  18. static    char *errtyp1 = "\nerror:  cannot interpret the %s in the %s date.\n";
  19. static    char *errtyp2 = "\nerror:  %s date is not a valid date.\n";
  20.  
  21. static    char *fldnme[3] = { "month", "day", "year" };    /* this is for the */
  22. static    char *dtenme[2] = { "first", "second" };    /* error output.   */
  23.  
  24. _setenvp()    { }                /* special for MSC */
  25.  
  26. main( argc, argv )
  27.  
  28. int      argc;
  29. char    **argv;
  30.  
  31. {
  32.     unsigned int    gregdte[3], i;        /* m, d, & y ( & index ) */
  33.     unsigned long    julian[2];        /* holds 2 julian dates  */
  34.     char        dtestr[29];        /* to hold verbose dates */
  35.     char        *nxtfld;        /* for dividing parms     */
  36.     unsigned int    chkdte[3];        /* used to check date     */
  37.  
  38.     if ( argc-- != 3 )            /* two parms input ? */
  39.     {
  40.          printf( "\n DAYS   is a program that calculates the number of days that" );
  41.          printf( "\n ====   exist between any two dates.\n" );
  42.          printf( "\nusage:  DAYS date1 date2" );
  43.          printf( "\n\nwhere:" );
  44.          printf( "\n\tdate1 is the starting date of the period, and" );
  45.          printf( "\n\tdate2 is the ending   date of the period." );
  46.          printf( "\n\n- Both dates MUST be input in the \"month/day/year\" format.\n" );
  47.  
  48.          return( 0 );
  49.     }
  50.  
  51.     /*******************************************************************/
  52.     /* The following loop converts the two input dates to julian longs */
  53.     /*******************************************************************/
  54.  
  55.     while ( argc )                    /* for both dates, */
  56.     {
  57.         for ( i = 0; i < 2; ++i )        /* pick off m & d  */
  58.         {
  59.               if ( gregdte[i] = atoi( argv[argc] ) )
  60.               {
  61.                if ( nxtfld = strstr( argv[argc], "/" ) )
  62.                {
  63.                 argv[argc] = ++nxtfld;
  64.                }
  65.                else
  66.                {
  67.                 printf( errtyp1, fldnme[++i], dtenme[--argc] );
  68.                 return( 1 );
  69.                }
  70.               }
  71.               else
  72.               {
  73.                printf( errtyp1, fldnme[i], dtenme[--argc] );
  74.                return( 1 );
  75.               }
  76.         }
  77.  
  78.         gregdte[i] = atoi( argv[argc] );    /* then pick off y */
  79.  
  80.         /*********************************************************/
  81.         /*   this line converts the m, d, & y to a JULIAN date   */
  82.         /*********************************************************/
  83.  
  84.         julian[--argc] = gtoj( gregdte[0], gregdte[1], gregdte[2] );
  85.  
  86.         /*****************************************************/
  87.         /*   these lines check to see if the date is valid   */
  88.         /*****************************************************/
  89.  
  90.         jtog( julian[argc], (char *)chkdte, 0 );
  91.  
  92.         for ( i = 0; i < 3; ++i )
  93.         {
  94.               if ( chkdte[i] != gregdte[i] )
  95.               {
  96.                printf( errtyp2, dtenme[argc] );
  97.                return( 1 );
  98.               }
  99.         }
  100.     }
  101.  
  102.     /*******************************************************************/
  103.     /*   this line computes the NUMBER OF DAYS between the two dates   */
  104.     /*******************************************************************/
  105.  
  106.     printf( "\nThere are %li days between the two dates,",
  107.          daycnt( julian[0], julian[1], 0 ) );
  108.  
  109.     /******************************************************/
  110.     /*   these lines create the VERBOSE GREGORIAN dates   */
  111.     /******************************************************/
  112.  
  113.     fulldte( julian[0], dtestr );
  114.  
  115.     printf( "\n\n\t  %s", dtestr );
  116.  
  117.     fulldte( julian[1], dtestr );
  118.  
  119.     printf( " and %s.", dtestr );
  120.  
  121.     /*******************************************************************/
  122.     /*   this line computes the weekday, Saturday, and Sunday counts   */
  123.     /*******************************************************************/
  124.  
  125.     printf( "\n\nOf these, %li are weekdays, %li are Saturdays, and %li are Sundays.\n",
  126.          daycnt( julian[0], julian[1], 2 ),
  127.          satcnt( julian[0], julian[1] ),
  128.          suncnt( julian[0], julian[1] )  );
  129.  
  130.     return( 0 );
  131. }
  132.